package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    ApartmentLabelMapper apartmentLabelMapper;

    @Autowired
    ApartmentFacilityMapper apartmentFacilityMapper;

    @Autowired
    ApartmentFeeValueMapper apartmentFeeValueMapper;

    @Autowired
    GraphInfoMapper graphInfoMapper;

    @Autowired
    ProvinceInfoMapper provinceInfoMapper;

    @Autowired
    CityInfoMapper cityInfoMapper;

    @Autowired
    DistrictInfoMapper districtInfoMapper;

    @Autowired
    FeeValueMapper feeValueMapper;

    @Autowired
    private LabelInfoMapper LabelInfoMapper;

    @Autowired
    private FacilityInfoMapper FacilityInfoMapper;


    @Autowired
    private ApartmentLabelService apartmentLabelService;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;





    //保存或更新
    @Transactional
    @Override
    public void saveOrUpdateApartmentSubmitVo(ApartmentSubmitVo apartmentSubmitVo) {
        Long apartmentId = apartmentSubmitVo.getId();// 有就是更新，没有就是保存

        // 封装区域地名
        Long provinceId = apartmentSubmitVo.getProvinceId();
        Long cityId = apartmentSubmitVo.getCityId();
        Long districtId = apartmentSubmitVo.getDistrictId();
        String provinceName = provinceInfoMapper.selectById(provinceId).getName();
        String cityName = cityInfoMapper.selectById(cityId).getName();
        String districtName = districtInfoMapper.selectById(districtId).getName();
        apartmentSubmitVo.setProvinceName(provinceName);
        apartmentSubmitVo.setCityName(cityName);
        apartmentSubmitVo.setDistrictName(districtName);

        this.saveOrUpdate(apartmentSubmitVo);
        // 需要填入的数据
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();

        if (apartmentId != null) {
            // 更新

            // 删除标签中间表
            QueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new QueryWrapper<>();
            apartmentLabelQueryWrapper.eq("apartment_id", apartmentId);
            apartmentLabelMapper.delete(apartmentLabelQueryWrapper);

            // 删除配套中间表
            QueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new QueryWrapper<>();
            apartmentFacilityQueryWrapper.eq("apartment_id", apartmentId);
            apartmentFacilityMapper.delete(apartmentFacilityQueryWrapper);

            // 删除杂费中间表
            QueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new QueryWrapper<>();
            apartmentFeeValueQueryWrapper.eq("apartment_id", apartmentId);
            apartmentFeeValueMapper.delete(apartmentFeeValueQueryWrapper);

            // 删除图片表
            QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
            graphInfoQueryWrapper.eq("item_type", ItemType.APARTMENT);
            graphInfoQueryWrapper.eq("item_id", apartmentId);
            graphInfoMapper.delete(graphInfoQueryWrapper);
        }

        // 插入新数据
        apartmentId = apartmentSubmitVo.getId();// 避免新增的时候第一次获取id为null

        // 标签中间表
        apartmentLabelMapper.insertBatch(apartmentId, labelIds);

        // 配套中间表
        apartmentFacilityMapper.insertBatch(apartmentId, facilityInfoIds);

        // 杂费中间表
        apartmentFeeValueMapper.insertBatch(apartmentId, feeValueIds);

        // 图片表
        graphInfoMapper.insertBatch(ItemType.APARTMENT, apartmentId, graphVoList);


    }

    //分页查询
    @Override
    public Page<ApartmentItemVo> pageItem(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        Page<ApartmentItemVo> apartmentItemVoPage = apartmentInfoMapper.pageItem(page, queryVo);
        return apartmentItemVoPage;
    }

    //根据公寓id获取公寓详情
    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);

        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        // 查询并设置标签信息
        List<LabelInfo> labelInfoList = LabelInfoMapper.selectListByApartmentId(id);
        apartmentDetailVo.setLabelInfoList(labelInfoList);

        // 查询并设置配套设施信息
        List<FacilityInfo> facilityInfoList = FacilityInfoMapper.selectListByApartmentId(id);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);

        // 查询并设置杂费信息
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);

        apartmentDetailVo.setFeeValueVoList(feeValueVoList);

        // 查询并设置图片信息
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);
        apartmentDetailVo.setGraphVoList(graphVoList);


        return apartmentDetailVo;
    }

    //根据公寓id删除公寓信息
    @Transactional
    @Override
    public void removeApartmentById(Long id) {

        //1.删除GraphInfo
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphQueryWrapper);

        //2.删除ApartmentLabel
        LambdaQueryWrapper<ApartmentLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
        labelQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(labelQueryWrapper);

        //3.删除ApartmentFacility
        LambdaQueryWrapper<ApartmentFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
        facilityQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(facilityQueryWrapper);

        //4.删除ApartmentFeeValue
        LambdaQueryWrapper<ApartmentFeeValue> feeQueryWrapper = new LambdaQueryWrapper<>();
        feeQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(feeQueryWrapper);

        super.removeById(id);

    }

    //根据区域id查询公寓信息
    @Override
    public List<ApartmentInfo> listInfoByDistrictId(Long id) {
        return apartmentInfoMapper.listInfoByDistrictId(id);
    }

    //根据公寓id修改发布状态
    @Override
    public void updateReleaseStatusById(Long id, ReleaseStatus status) {
        apartmentInfoMapper.updateReleaseStatusById(id, status);

    }


}




